package com.zy.socket.chat_0;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @Author Gong
 * @Description
 * @dATE 2021/12/19 18:59
 **/
public class  Client {
    Socket socket;
    BufferedWriter bw;
    BufferedReader br ;

    public Client(String ip, int port){
        try {
            socket =new Socket(ip,port);
            bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        }catch (UnknownHostException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    public void sendMessage(String message) {
        try{
            bw.write(message);
            bw.newLine();
            bw.flush();
        }catch(IOException e){
            e.printStackTrace();

        }
    }

    public String reciveMessage() {
        String message=null;
        try {
            message = br.readLine();
        }catch(IOException e) {
            e.printStackTrace();
        }
        return message;
    }

    public void close(){
        try{
            socket.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }}